package com.fw.util;

import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

public class HttpClientSendRequestUtils {

    public static final int POST_REQUEST = 1;
    public static final int GET_REQUEST = 2;

    public static String sendRequest(int type, String ipHost, String sendToPath, int port, Map<String, String> parameters) {
	String result = null;
	
	HttpParams httpParameters = new BasicHttpParams();
	HttpConnectionParams.setConnectionTimeout(httpParameters, 1200000);
	HttpConnectionParams.setSoTimeout(httpParameters, 1200000);
	HttpConnectionParams.setTcpNoDelay(httpParameters, true);
	HttpConnectionParams.setSoKeepalive(httpParameters, true);
	HttpClient httpclient = new DefaultHttpClient(httpParameters);

	org.apache.http.client.methods.HttpRequestBase requestBase = null;
	try {
	    HttpResponse httpResponse = null;
	    if (GET_REQUEST == type) {
		URIBuilder builder = new URIBuilder();
		builder = builder.setScheme("http").setHost(ipHost).setPort(port).setPath(sendToPath);
		if (parameters != null && !parameters.isEmpty()) {
		    for (Entry<String, String> entry : parameters.entrySet()) {
			builder = builder.setParameter(entry.getKey(), entry.getValue());
		    }
		}
		URI uri = builder.build();
		requestBase = new HttpGet(uri);
		httpResponse = httpclient.execute(requestBase);
	    } else if (POST_REQUEST == type) {
		HttpHost target = new HttpHost(ipHost, port);
		requestBase = new HttpPost(sendToPath);
		if (parameters != null && !parameters.isEmpty()) {
		    List<NameValuePair> formparams = new ArrayList<NameValuePair>();
		    for (Entry<String, String> entry : parameters.entrySet()) {
			formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
		    }
		    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
		    ((HttpPost) requestBase).setEntity(entity);
		}
		httpResponse = httpclient.execute(target, requestBase);
	    }
	    HttpEntity entity = null;
	    if (httpResponse != null) {
		entity = httpResponse.getEntity();
	    }
	    if (entity != null) {
		result = EntityUtils.toString(entity);
		return result;
	    } else {
		result = "entity : " + null;
		return result;
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	    result = "Exception in sendRequest";
	    return result;
	} finally {
	    if (requestBase != null) {
		requestBase.releaseConnection();
	    }
	    if (httpclient != null && httpclient.getConnectionManager() != null) {
		httpclient.getConnectionManager().shutdown();
	    }
	}
    }

    public static void main(String[] args) throws Exception {
	String ipHost = "192.168.3.208";
	String sendToPath = "/PMSProject/GenerateMonitorServlet";
	int port = 8080;
	Map<String, String> parameters = new HashMap<String, String>();
	parameters.put("projectid", "10P1535");
	parameters.put("userid", "1");

	System.out.println("Send POST REQ = "
		+ HttpClientSendRequestUtils.sendRequest(HttpClientSendRequestUtils.POST_REQUEST, ipHost, sendToPath, port, parameters));
	System.out.println("Send GET REQ = "
		+ HttpClientSendRequestUtils.sendRequest(HttpClientSendRequestUtils.GET_REQUEST, ipHost, sendToPath, port, parameters));
    }

}
